//
//  SDBrowserImageView.m
//  SDPhotoBrowser
//
//  Created by aier on 15-2-6.
//  Copyright (c) 2015年 GSD. All rights reserved.
//

#import "SDBrowserImageView.h"
#import "UIImageView+WebCache.h"
#import "SDPhotoBrowserConfig.h"

@implementation SDBrowserImageView
{
    SDWaitingView *_waitingView;
    BOOL _didCheckSize;
    UIScrollView *_scroll;
 
    UIView *_containerView;
}


- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
      [self _setup];
    }
    return self;
}
- (void)_setup {
  self.clipsToBounds = YES;
  self.contentMode = UIViewContentModeScaleAspectFill;
  
  _scroll = [[UIScrollView alloc] initWithFrame:self.bounds];
  _scroll.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
  _scroll.showsHorizontalScrollIndicator = NO;
  _scroll.showsVerticalScrollIndicator = NO;
  _scrollImageView = [UIImageView new];
  [_containerView addSubview:_scrollImageView];
  _scroll.delegate = self;
  
  _containerView = [[UIView alloc] initWithFrame:self.bounds];
  [_scroll addSubview:_containerView];
  [self addSubview:_scroll];
}

- (BOOL)isScaled
{
    return (_scroll.zoomScale != _scroll.minimumZoomScale);
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    _waitingView.center = CGPointMake(self.frame.size.width * 0.5, self.frame.size.height * 0.5);
    
}



- (void)setProgress:(CGFloat)progress
{
    _progress = progress;
    _waitingView.progress = progress;

}
- (void) setImage:(UIImage *)image {
  _scrollImageView.image = image;
}

- (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder
{
    SDWaitingView *waiting = [[SDWaitingView alloc] init];
    waiting.bounds = CGRectMake(0, 0, 100, 100);
    waiting.mode = SDWaitingViewProgressMode;
    _waitingView = waiting;
    [self addSubview:waiting];
    
    
    __weak SDBrowserImageView *imageViewWeak = self;

    [_scrollImageView sd_setImageWithURL:url placeholderImage:placeholder options:SDWebImageRetryFailed progress:^(NSInteger receivedSize, NSInteger expectedSize) {
        
        imageViewWeak.progress = (CGFloat)receivedSize / expectedSize;
        
    } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        
        [imageViewWeak removeWaitingView];
        
        if (error) {
            UILabel *label = [[UILabel alloc] init];
            label.bounds = CGRectMake(0, 0, 160, 30);
            label.center = CGPointMake(imageViewWeak.bounds.size.width * 0.5, imageViewWeak.bounds.size.height * 0.5);
            label.text = @"图片加载失败";
            label.font = [UIFont systemFontOfSize:16];
            label.textColor = [UIColor whiteColor];
            label.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.8];
            label.layer.cornerRadius = 5;
            label.clipsToBounds = YES;
            label.textAlignment = NSTextAlignmentCenter;
            [imageViewWeak addSubview:label];
        } else {
          [self imageDidChange];
        }
   
    }];
}
- (void)imageDidChange {
  CGSize size = (_scrollImageView.image) ? _scrollImageView.image.size : self.bounds.size;
  CGFloat ratio = MIN(_scroll.frame.size.width / size.width, _scroll.frame.size.height / size.height);
  CGFloat W = ratio * size.width;
  CGFloat H = ratio * size.height;
  _scrollImageView.frame = CGRectMake(0, 0, W, H);
  
  _scroll.zoomScale = 1;
  _scroll.contentOffset = CGPointZero;
  _containerView.bounds = _scrollImageView.bounds;
  
  [self resetZoomScale];
  _scroll.zoomScale  = _scroll.minimumZoomScale;
  [self scrollViewDidZoom:_scroll];
}

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
  return _containerView;
}

- (void)scrollViewDidZoom:(UIScrollView *)scrollView {
  CGFloat Ws = _scroll.frame.size.width - _scroll.contentInset.left - _scroll.contentInset.right;
  CGFloat Hs = _scroll.frame.size.height - _scroll.contentInset.top - _scroll.contentInset.bottom;
  CGFloat W = _containerView.frame.size.width;
  CGFloat H = _containerView.frame.size.height;
  
  CGRect rct = _containerView.frame;
  rct.origin.x = MAX((Ws-W)/2, 0);
  rct.origin.y = MAX((Hs-H)/2, 0);
  _containerView.frame = rct;
}

- (void)resetZoomScale {
  CGFloat Rw = _scroll.frame.size.width / _scrollImageView.frame.size.width;
  CGFloat Rh = _scroll.frame.size.height / _scrollImageView.frame.size.height;
  
  CGFloat scale = 1;
  Rw = MAX(Rw, _scrollImageView.image.size.width / (scale * _scroll.frame.size.width));
  Rh = MAX(Rh, _scrollImageView.image.size.height / (scale * _scroll.frame.size.height));
  
  _scroll.contentSize = _scrollImageView.frame.size;
  _scroll.minimumZoomScale = 1;
  _scroll.maximumZoomScale = MAX(MAX(Rw, Rh), 1);
}


- (void)removeWaitingView
{
    [_waitingView removeFromSuperview];
}


@end
// 版权属于原作者
// http://code4app.com (cn) http://code4app.net (en)
// 发布代码于最专业的源码分享网站: Code4App.com